package com.spring.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.spring.service")
@EnableAspectJAutoProxy
@EnableTransactionManagement
@Import(DBConfig.class)
public class SpringConfig {
    //1、配置sqlSessionFactionBean
  @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
      SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
      factoryBean.setDataSource(dataSource);
      factoryBean.setTypeAliasesPackage("com.spring.dmain");
      return factoryBean;
  }
  //2、扫描mapper
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.spring.mapper");
        return mapperScannerConfigurer;
    }
    //设置事务管理
    public TransactionManager transactionManager(DataSource dataSource){
      return transactionManager(dataSource);
    }
}
